﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Manufacture
{
    public partial class frmMaintainWorkingGroup : BaseForm.frmBaseDialogyMaintainForm
    {
        #region vars
        public MstWorkingGroupEntity SelectedMstWorkingGroup { set; get; }
        private List<MstPersonEntity> SelectedSourceMstPerson = new List<MstPersonEntity>();
        private List<MstPersonEntity> SelectedTargetMstPerson = new List<MstPersonEntity>();
        #endregion

        #region ctors
        public frmMaintainWorkingGroup()
        {
            InitializeComponent();
        }
        #endregion

        #region functions
        private void BindComobox()
        {
            List<MstDepartmentEntity> ListMstDepartment = MstDepartmentBs.GetDepartmentListAll(false);
            cmbDtp.DisplayMember = "DepartmentName";
            cmbDtp.ValueMember = "DepartmentId";
            cmbDtp.DataSource = ListMstDepartment;
        }

        private void BindGridDataPersons()
        {
            if (!string.IsNullOrEmpty(cmbDtp.SelectedValue.ToString()))
            {
                List<MstPersonEntity> ListMstPerson = MstPersonBs.GetPersonByDepartment(cmbDtp.SelectedValue.ToString());
                gvPersons.DataSource = ListMstPerson;
            }
        }

        private void SetField()
        {
            if (SelectedMstWorkingGroup == null)
            { return; }
            txtGroupName.Text = SelectedMstWorkingGroup.GroupName;
            MstPersonEntity m = MstPersonBs.GetMstPersonByID(SelectedMstWorkingGroup.GroupLeader);
            txtLeador.Text = m.NameCn;
            txtLeador.Tag = m;
            txtRemark.Text = SelectedMstWorkingGroup.Remark;
        }

        private void LoadGridTarget()
        {
            if (SelectedMstWorkingGroup == null)
                return;
            List<MstWorkingGroupPersonEntity> list = MstWorkingGroupPersonBs.GetListMstWorkingGroupPersonByGroupID(SelectedMstWorkingGroup.GroupId);
            if (list == null || list.Count <= 0)
                return;
            List<MstPersonEntity> listWorkingGroupPerson = new List<MstPersonEntity>();
            foreach (MstWorkingGroupPersonEntity m in list)
            {
                MstPersonEntity t = MstPersonBs.GetMstPersonByID(m.PersonId);
                listWorkingGroupPerson.Add(t);
            }
            gvGroupPersons.DataSource = listWorkingGroupPerson;
        }

        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtGroupName.Text))
            {
                MessageBox.Show("组名不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtGroupName.Focus();
                return false;
            }
            List<MstWorkingGroupEntity> ListMstWorkingGroup = MstWorkingGroupBs.GetMstWorkingGroupAllList();
            MstWorkingGroupEntity n = ListMstWorkingGroup.Find(delegate(MstWorkingGroupEntity m)
            {
                return m.GroupId == txtGroupName.Text;
            });
            if (n != null)
            {
                MessageBox.Show("组名已存在！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtGroupName.Text = "";
                txtGroupName.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtLeador.Text))
            {
                MessageBox.Show("组长不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtLeador.Focus();
                return false;
            }
            if (gvGroupPersons.DataSource == null)
            {
                MessageBox.Show("组员不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            DataTable dt = gvGroupPersons.DataSource as DataTable;
            //if (dt.Rows.Count == 0)
            //{
            //    MessageBox.Show("组员不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return false;
            //}

            List<MstPersonEntity> groupPersons = (List<MstPersonEntity>)gvGroupPersons.DataSource;
            if (groupPersons.Count == 0)
            {
                MessageBox.Show("组员不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            return true;
        }

        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindComobox();
            if (this.ThisActionType == ActionType.View)
            {
                btnSave.Enabled = false;
                btnCancel.Text = "关闭";
                btnCancel.Enabled = true;
                SetField();
                LoadGridTarget();
                gvGroupPersons.Enabled = false;
            }
            if (this.ThisActionType == ActionType.Modify)
            {
                SetField();
                LoadGridTarget();
            }
        }

        private void cmbDtp_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGridDataPersons();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (gridView2.GetSelectedRows().Length == 0)
            {
                return;
            }
            List<MstPersonEntity> listSource = (List<MstPersonEntity>)gvPersons.DataSource;
            foreach (int nIndex in gridView2.GetSelectedRows())
            {
                SelectedSourceMstPerson.Add(listSource[nIndex]);
            }
            List<MstPersonEntity> ListMstPerson = (List<MstPersonEntity>)gvGroupPersons.DataSource;
            foreach (MstPersonEntity m in SelectedSourceMstPerson)
            {
                if (ListMstPerson != null && ListMstPerson.Count > 0)
                {
                    if (ListMstPerson.Contains(m))
                        continue;
                    else
                    {
                        ListMstPerson.Add(m);
                    }
                }
                else
                {
                    ListMstPerson = new List<MstPersonEntity>();
                    ListMstPerson.Add(m);
                }
            }

            gvGroupPersons.DataSource = null;
            gvGroupPersons.DataSource = ListMstPerson;
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (gridView1.GetSelectedRows().Length == 0)
            {
                return;
            }
            List<MstPersonEntity> listTarget = (List<MstPersonEntity>)gvGroupPersons.DataSource;
            foreach (int nIndex in gridView1.GetSelectedRows())
            {
                SelectedTargetMstPerson.Add(listTarget[nIndex]);
            }
            List<MstPersonEntity> ListMstPerson = (List<MstPersonEntity>)gvGroupPersons.DataSource;
            foreach (MstPersonEntity m in SelectedTargetMstPerson)
            {
                ListMstPerson.Remove(m);
            }
            gvGroupPersons.DataSource = null;
            gvGroupPersons.DataSource = ListMstPerson;
        }

        protected override void Click_Save()
        {
            base.Click_Save();
            if (CheckData())
            {
                if (SelectedMstWorkingGroup == null)
                {
                    SelectedMstWorkingGroup = new MstWorkingGroupEntity();
                    SelectedMstWorkingGroup.GroupId = Guid.NewGuid().ToString();
                }
                SelectedMstWorkingGroup.GroupName = txtGroupName.Text;
                SelectedMstWorkingGroup.GroupLeader = ((MstPersonEntity)txtLeador.Tag).PersonId;
                SelectedMstWorkingGroup.Remark = txtRemark.Text;

                List<MstWorkingGroupPersonEntity> list = new List<MstWorkingGroupPersonEntity>();
                List<MstPersonEntity> ListMstPerson = (List<MstPersonEntity>)gvGroupPersons.DataSource;
                foreach (MstPersonEntity m in ListMstPerson)
                {
                    MstWorkingGroupPersonEntity n = new MstWorkingGroupPersonEntity();
                    n.GroupId = SelectedMstWorkingGroup.GroupId;
                    n.PersonId = m.PersonId;
                    list.Add(n);
                }
                //if (ThisActionType == ActionType.New)
                //{  MstWorkingGroupEntity  entity=new MstWorkingGroupEntity ();
                //    bool isSuccessful = MstWorkingGroupBs.InsertMstWorkingGroup(entity);
                //    if (!isSuccessful)
                //    {
                //        MessageBox.Show("新增失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                //    }
                //    else
                //    {
                //        this.DialogResult = DialogResult.OK;
                //    }
                //}
                if (ThisActionType == ActionType.Modify)
                {
                    bool isSuccessful = MstWorkingGroupBs.UpdateMstWorkingGroup(SelectedMstWorkingGroup);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    }
                   
                }
                MstWorkingGroupEntity mMstWorkingGroup = MstWorkingGroupBs.GetMstWorkingGroup(SelectedMstWorkingGroup);
                bool isNew = false;
                if (mMstWorkingGroup == null)
                    isNew = true;
                if (MstWorkingGroupBs.SaveWorkingGroups(isNew, SelectedMstWorkingGroup, list))
                {
                    MessageBox.Show("保存成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
        }

        protected override void Click_Cancel()
        {
            base.Click_Cancel();
            this.Close();
        }

        private void txtLeador_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtLeador.Text = frm.SelectedPerson.NameCn;
                txtLeador.Tag = frm.SelectedPerson;
            }
            else
            {
                txtLeador.Text = "";
                txtLeador.Tag = null;
            }
        }
        #endregion
    }
}
